#include "InputManager.h"

#include <iostream>

bool	InputManager :: enterPressed		= false;
bool	InputManager :: enterIsDown			= false;
bool	InputManager :: upArrowPressed		= false;
bool	InputManager :: upArrowIsDown		= false;
bool	InputManager :: downArrowPressed	= false;
bool	InputManager :: downArrowIsDown		= false;
bool	InputManager :: leftArrowPressed	= false;
bool	InputManager :: leftArrowIsDown		= false;
bool	InputManager :: rightArrowPressed	= false;
bool	InputManager :: rightArrowIsDown	= false;
bool	InputManager :: mousePressed		= false;
bool	InputManager :: mouseIsDown			= false;
float	InputManager :: horizontalAxis		= 0.0f;
float	InputManager :: verticalAxis		= 0.0f;

using namespace sf;

void InputManager :: UpdateInputs(Window* window, float deltaTime)
{
	Event e;

	bool enter	= false;
	bool up		= false;
	bool down	= false;
	bool left	= false;
	bool right	= false;
	bool mouse	= false;

	enterPressed		= false;
	upArrowPressed		= false;
	downArrowPressed	= false;
	leftArrowPressed	= false;
	rightArrowPressed	= false;
	mousePressed		= false;


	while (window->pollEvent(e))
	{
		//close window id necassary
		if (Event::Closed == e.type || e.key.code == Keyboard::Escape)
		{
			window->close();
		}

		//Record if any of the required buttons have been pressed
		if (e.key.code == Keyboard::Return)
		{
			if (e.type == sf::Event::KeyPressed)
			{
				if (!enterIsDown)
					enterPressed = true;
				enterIsDown = true;
			}
			else if (e.type == sf::Event::KeyReleased)
			{
				enterIsDown = false;
			}

		}
		else if (e.key.code == Keyboard::Left || e.key.code == Keyboard::A)
		{
			if (e.type == sf::Event::KeyPressed)
			{
				if (!leftArrowIsDown)
					leftArrowPressed = true;
				leftArrowIsDown=true;
			}
			else if (e.type == sf::Event::KeyReleased)
			{
				leftArrowIsDown = false;
			}
			
		}
		else if (e.key.code == Keyboard::Right || e.key.code == Keyboard::D)
		{
			if (e.type == sf::Event::KeyPressed)
			{
				if (!rightArrowIsDown)
					rightArrowPressed = true;
				rightArrowIsDown = true;
			}
			else if (e.type == sf::Event::KeyReleased)
			{
				rightArrowIsDown = false;
			}
		}
		else if (e.key.code == Keyboard::Up || e.key.code == Keyboard::W)
		{
			if (e.type == sf::Event::KeyPressed)
			{
				if (!upArrowIsDown)
					upArrowPressed = true;
				upArrowIsDown = true;
			}
			else if (e.type == sf::Event::KeyReleased)
			{
				upArrowIsDown = false;
			}
		}
		else if (e.key.code == Keyboard::Down || e.key.code == Keyboard::S)
		{
			if (e.type == sf::Event::KeyPressed)
			{
				if (!downArrowIsDown)
					downArrowPressed = true;
				downArrowIsDown = true;
			}
			else if (e.type == sf::Event::KeyReleased)
			{
				downArrowIsDown = false;
			}
		}
		else if (e.key.code == Mouse::Left)
		{
			if (e.type == sf::Event::MouseButtonPressed)
			{
				if (!mouseIsDown)
					mousePressed = true;
				mouseIsDown = true;
			}
			else if (e.type == sf::Event::MouseButtonReleased)
			{
				mouseIsDown = false;
			}


			mouse = true;
			if (!mouseIsDown)
				mousePressed = true;
			mouseIsDown = true;
		}
	}


	//update Horizontal Axis variables
	if (!leftArrowIsDown && !rightArrowIsDown)
	{
		if (horizontalAxis != 0.0f)
		{
			if (horizontalAxis < -0.01f)
			{
				horizontalAxis += 5.0f * deltaTime;
			}
			else if (horizontalAxis > 0.01f)
			{
				horizontalAxis -= 5.0f * deltaTime;

			}
			else
				horizontalAxis = 0.0f;
		}
	}
	else
	{
		if (leftArrowIsDown && horizontalAxis > -1.0f)
		{
			horizontalAxis -= 5.0f * deltaTime;
			if (horizontalAxis < -1.0f)
				horizontalAxis = -1.0f;
		}
		if (rightArrowIsDown && horizontalAxis < 1.0f)
		{
			horizontalAxis += 5.0f * deltaTime;
			if (horizontalAxis > 1.0f)
				horizontalAxis = 1.0f;
		}
	}

	//update Vertical Axis variables
	if (!upArrowIsDown && !downArrowIsDown)
	{
		if (verticalAxis != 0.0f)
		{
			if (verticalAxis < -0.01f)
			{
				verticalAxis += 5.0f * deltaTime;
			}
			else if (verticalAxis > 0.01f)
			{
				verticalAxis -= 5.0f * deltaTime;

			}
			else
				verticalAxis = 0.0f;
		}
	}
	else
	{
		if (downArrowIsDown && verticalAxis > -1.0f)
		{
			verticalAxis -= 5.0f * deltaTime;
			if (verticalAxis < -1.0f)
				verticalAxis = -1.0f;
		}
		if (upArrowIsDown && verticalAxis < 1.0f)
		{
			verticalAxis += 5.0f * deltaTime;
			if (verticalAxis > 1.0f)
				verticalAxis = 1.0f;
		}
	}
}